<?php
namespace addons\oil\model;

use think\Model;
use addons\oil\exception\Exception;
use addons\oil\library\traits\model\order\OrderScope;
use think\Db;
use think\Queue;
use traits\model\SoftDelete;
use addons\oil\library\traits\model\order\OrderOper;
use addons\oil\library\traits\model\order\OrderStatus;

/**
 * 订单模型
 */
class Order extends Model
{
    use SoftDelete, OrderOper, OrderScope, OrderStatus;

    // 表名,不含前缀
    protected $name = 'oil_order';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    protected $hidden = ['updatetime', 'deletetime'];
    // //列表动态隐藏字段
    // protected static $list_hidden = ['content', 'params', 'images', 'service_ids'];

    // // 追加属性
    protected $append = [
        'status_code',
        'status_name',
        'status_desc',
        'btns',
        'ext_arr',
        'user_name',
        'is_invalid'
    ];

    // 订单状态
    const STATUS_INVALID = -2;//交易关闭
    const STATUS_CANCEL = -1; //已取消
    const STATUS_NOPAY = 0; //未支付
    const STATUS_PAYED = 1; //已支付
    const STATUS_FINISH = 2; //已完成
    const STATUS_SHARE = 3; //已转赠

    // 售后状态
    const AFTERSALE_STATUS_REFUSE = -1;       // 驳回
    const AFTERSALE_STATUS_NOAFTER = 0;       // 未申请
    const AFTERSALE_STATUS_AFTERING = 1;       // 申请退款
    const AFTERSALE_STATUS_OK = 2;        // 售后完成

    // 退款状态
    const REFUND_STATUS_REFUSE = -1;       // 拒绝退款
    const REFUND_STATUS_NOREFUND = 0;       // 退款状态 未申请
    const REFUND_STATUS_ING = 1;       // 申请中    // 不需要申请（状态不会出现）
    const REFUND_STATUS_OK = 2;       // 已同意
    const REFUND_STATUS_FINISH = 3;       // 退款完成

    const HEXIAO_STATUS_NO = 0;       // 未核销
    const HEXIAO_STATUS_FINISH = 1;       // 已核销

    /* -------------------------- 访问器 ------------------------ */

    public function getExtArrAttr($value, $data)
    {
        $ext = (isset($data['ext']) && $data['ext']) ? json_decode($data['ext'], true) : [];
        return $ext;
    }
    protected function getStatus($data, $type)
    {
        $btns = [];
        $status_name = '';
        $status_desc = '';

        switch ($this->status_code) {
            case 'cancel':
                $status_name = '已取消';
                $status_desc = '订单已取消';
                $btns[] = 'delete';     // 删除订单
                break;
            case 'invalid':
                $status_name = '交易关闭';
                $status_desc = '交易关闭';
                $btns[] = 'delete';     // 删除订单
                break;
            case 'nopay':
                $status_name = '待付款';
                $status_desc = '等待买家付款';
                $btns[] = 'cancel';     // 取消订单
                $btns[] = 'pay';        // 支付
                break;

            // 已支付
            case 'payed':
                $status_name = '已支付';
                $status_desc = '订单已支付';
                $btns[] = 'refund';        // 申请退款
                break;
            case 'refund_finish':
                $status_name = '退款完成';
                $status_desc = '订单退款完成';
                break;
            case 'refund_ing':
                $status_name = '退款处理中';
                $status_desc = '退款处理中';
                break;
            case 'refund_refuse':
                $status_name = '退款已拒绝';
                $status_desc = '退款已拒绝';
                break;
            // 已支付的结束
            case 'finish':
                $status_name = '交易完成';
                $status_desc = '交易完成';
                $btns[] = 'delete';     // 删除订单
                break;
             // 转赠出去的订单
            case 'share':
                $status_name = '已转赠';
                $status_desc = '已转赠';
                $btns[] = 'delete';     // 删除订单
                break;
        }
        return $type == 'status_name' ? $status_name : ($type == 'btns' ? $btns : $status_desc);
    }

    public function getIsInvalidAttr($value, $data)
    {
        $is_is_invalid=0;
        $hadtimes=time()-$data['createtime'];
        $six_mouth_times=86400*30*6;
        if($hadtimes>$six_mouth_times){
            $is_is_invalid=1;
        }
        return $is_is_invalid;
    }
    public function getUserNameAttr($value, $data)
    {
        $username=\think\Db::name('user')->where('id', $data['user_id'])->value('nickname');
        return $username??'';
    }
    /* -------------------------- 访问器 ------------------------ */



    /* -------------------------- 模型关联 ------------------------ */

    /* -------------------------- 模型关联 ------------------------ */
}

